<?php

declare(strict_types=1);

use think\migration\Migrator;

class CreateOrderTables extends Migrator
{
    public function change(): void
    {
        $this->createOrdersTable();
        $this->createOrderItemsTable();
        $this->createOrderPaymentsTable();
        $this->createOrderRefundsTable();
        $this->createOrderLogsTable();
    }

    private function createOrdersTable(): void
    {
        $this->table('orders', [
            'comment' => '订单主表',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('order_no', 'string', [
                'limit' => 60,
                'comment' => '订单编号',
            ])
            ->addColumn('buyer_user_id', 'integer', [
                'signed' => false,
                'comment' => '买家用户ID',
            ])
            ->addColumn('buyer_org_id', 'integer', [
                'signed' => false,
                'comment' => '买家组织ID',
            ])
            ->addColumn('seller_user_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '卖家负责人用户ID',
            ])
            ->addColumn('seller_org_id', 'integer', [
                'signed' => false,
                'comment' => '卖家组织ID',
            ])
            ->addColumn('inviter_user_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '邀请人用户ID',
            ])
            ->addColumn('service_user_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '客服用户ID',
            ])
            ->addColumn('status', 'tinyinteger', [
                'signed' => false,
                'default' => 0,
                'comment' => '订单状态',
            ])
            ->addColumn('buyer_payable_amount', 'decimal', [
                'precision' => 12,
                'scale' => 2,
                'default' => 0,
                'comment' => '买家应付金额',
            ])
            ->addColumn('buyer_paid_amount', 'decimal', [
                'precision' => 12,
                'scale' => 2,
                'default' => 0,
                'comment' => '买家已付金额',
            ])
            ->addColumn('seller_receivable_amount', 'decimal', [
                'precision' => 12,
                'scale' => 2,
                'default' => 0,
                'comment' => '卖家应收金额',
            ])
            ->addColumn('platform_commission_amount', 'decimal', [
                'precision' => 12,
                'scale' => 2,
                'default' => 0,
                'comment' => '平台佣金',
            ])
            ->addColumn('amount_snapshot', 'json', [
                'null' => true,
                'comment' => '金额快照',
            ])
            ->addColumn('status_history', 'json', [
                'null' => true,
                'comment' => '状态流转记录',
            ])
            ->addColumn('submitted_at', 'timestamp', [
                'null' => true,
                'comment' => '提交时间',
            ])
            ->addColumn('paid_at', 'timestamp', [
                'null' => true,
                'comment' => '支付时间',
            ])
            ->addColumn('shipped_at', 'timestamp', [
                'null' => true,
                'comment' => '发货时间',
            ])
            ->addColumn('finished_at', 'timestamp', [
                'null' => true,
                'comment' => '完成时间',
            ])
            ->addColumn('cancelled_at', 'timestamp', [
                'null' => true,
                'comment' => '取消时间',
            ])
            ->addColumn('cancelled_reason', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '取消原因',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['order_no'], ['unique' => true])
            ->addIndex(['buyer_user_id'])
            ->addIndex(['seller_org_id'])
            ->addIndex(['service_user_id'])
            ->addIndex(['status'])
            ->create();
    }

    private function createOrderItemsTable(): void
    {
        $this->table('order_items', [
            'comment' => '订单商品明细',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('order_id', 'integer', [
                'signed' => false,
                'comment' => '订单ID',
            ])
            ->addColumn('product_id', 'integer', [
                'signed' => false,
                'comment' => '商品ID',
            ])
            ->addColumn('sku_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => 'SKU ID',
            ])
            ->addColumn('product_snapshot', 'json', [
                'null' => true,
                'comment' => '商品快照',
            ])
            ->addColumn('unit_price', 'decimal', [
                'precision' => 12,
                'scale' => 2,
                'default' => 0,
                'comment' => '单价',
            ])
            ->addColumn('qty', 'integer', [
                'default' => 1,
                'comment' => '数量',
            ])
            ->addColumn('seller_amount', 'decimal', [
                'precision' => 12,
                'scale' => 2,
                'default' => 0,
                'comment' => '卖家金额',
            ])
            ->addColumn('buyer_amount', 'decimal', [
                'precision' => 12,
                'scale' => 2,
                'default' => 0,
                'comment' => '买家金额',
            ])
            ->addColumn('platform_commission', 'decimal', [
                'precision' => 12,
                'scale' => 2,
                'default' => 0,
                'comment' => '平台佣金',
            ])
            ->addColumn('discount_amount', 'decimal', [
                'precision' => 12,
                'scale' => 2,
                'default' => 0,
                'comment' => '折扣金额',
            ])
            ->addColumn('status', 'tinyinteger', [
                'signed' => false,
                'default' => 1,
                'comment' => '状态',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['order_id'])
            ->addIndex(['product_id'])
            ->addIndex(['sku_id'])
            ->create();
    }

    private function createOrderPaymentsTable(): void
    {
        $this->table('order_payments', [
            'comment' => '订单支付记录',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('order_id', 'integer', [
                'signed' => false,
                'comment' => '订单ID',
            ])
            ->addColumn('pay_no', 'string', [
                'limit' => 60,
                'comment' => '支付流水号',
            ])
            ->addColumn('pay_method', 'string', [
                'limit' => 30,
                'comment' => '支付方式',
            ])
            ->addColumn('pay_amount', 'decimal', [
                'precision' => 12,
                'scale' => 2,
                'comment' => '支付金额',
            ])
            ->addColumn('currency', 'string', [
                'limit' => 10,
                'default' => 'CNY',
                'comment' => '币种',
            ])
            ->addColumn('status', 'tinyinteger', [
                'signed' => false,
                'default' => 0,
                'comment' => '状态',
            ])
            ->addColumn('third_party_no', 'string', [
                'limit' => 80,
                'default' => '',
                'comment' => '第三方流水号',
            ])
            ->addColumn('pay_time', 'timestamp', [
                'null' => true,
                'comment' => '支付时间',
            ])
            ->addColumn('refund_time', 'timestamp', [
                'null' => true,
                'comment' => '退款时间',
            ])
            ->addColumn('pay_data', 'json', [
                'null' => true,
                'comment' => '支付附加数据',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['order_id'])
            ->addIndex(['pay_no'], ['unique' => true])
            ->create();
    }

    private function createOrderRefundsTable(): void
    {
        $this->table('order_refunds', [
            'comment' => '订单售后/退款',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('order_id', 'integer', [
                'signed' => false,
                'comment' => '订单ID',
            ])
            ->addColumn('order_item_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '订单明细ID',
            ])
            ->addColumn('refund_no', 'string', [
                'limit' => 60,
                'comment' => '退款单号',
            ])
            ->addColumn('refund_type', 'string', [
                'limit' => 20,
                'default' => 'refund',
                'comment' => '退类型',
            ])
            ->addColumn('reason', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '申请原因',
            ])
            ->addColumn('apply_amount', 'decimal', [
                'precision' => 12,
                'scale' => 2,
                'comment' => '申请金额',
            ])
            ->addColumn('approved_amount', 'decimal', [
                'precision' => 12,
                'scale' => 2,
                'default' => 0,
                'comment' => '审核金额',
            ])
            ->addColumn('buyer_amount', 'decimal', [
                'precision' => 12,
                'scale' => 2,
                'default' => 0,
                'comment' => '退还买家金额',
            ])
            ->addColumn('seller_amount', 'decimal', [
                'precision' => 12,
                'scale' => 2,
                'default' => 0,
                'comment' => '卖家承担金额',
            ])
            ->addColumn('platform_amount', 'decimal', [
                'precision' => 12,
                'scale' => 2,
                'default' => 0,
                'comment' => '平台承担金额',
            ])
            ->addColumn('status', 'tinyinteger', [
                'signed' => false,
                'default' => 0,
                'comment' => '状态',
            ])
            ->addColumn('applicant_id', 'integer', [
                'signed' => false,
                'comment' => '申请人ID',
            ])
            ->addColumn('approver_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '审核人ID',
            ])
            ->addColumn('approve_time', 'timestamp', [
                'null' => true,
                'comment' => '审核时间',
            ])
            ->addColumn('complete_time', 'timestamp', [
                'null' => true,
                'comment' => '完成时间',
            ])
            ->addColumn('remark', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '备注',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['order_id'])
            ->addIndex(['order_item_id'])
            ->addIndex(['refund_no'], ['unique' => true])
            ->addIndex(['status'])
            ->create();
    }

    private function createOrderLogsTable(): void
    {
        $this->table('order_logs', [
            'comment' => '订单操作日志',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('order_id', 'integer', [
                'signed' => false,
                'comment' => '订单ID',
            ])
            ->addColumn('operator_user_id', 'integer', [
                'signed' => false,
                'comment' => '操作人ID',
            ])
            ->addColumn('action', 'string', [
                'limit' => 50,
                'comment' => '动作',
            ])
            ->addColumn('before_status', 'string', [
                'limit' => 30,
                'default' => '',
                'comment' => '变更前状态',
            ])
            ->addColumn('after_status', 'string', [
                'limit' => 30,
                'default' => '',
                'comment' => '变更后状态',
            ])
            ->addColumn('remark', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '备注',
            ])
            ->addColumn('payload', 'json', [
                'null' => true,
                'comment' => '扩展数据',
            ])
            ->addTimestamps()
            ->addIndex(['order_id'])
            ->addIndex(['operator_user_id'])
            ->create();
    }
}
